﻿namespace ExamV5.WorkExam
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class FrmAddWorkPaper : Page
    {
        protected Button butDefault;
        protected Button butShowRubricCount;
        protected DropDownList DDLEndH;
        protected DropDownList DDLEndM;
        protected DropDownList DDLStartH;
        protected DropDownList DDLStartM;
        protected HtmlForm Form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        protected int intID = 0;
        protected Label labChoose;
        protected Label labSumA;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected string strSubjectID = "";
        protected string strSubjectName = "";
        protected TextBox txtEndDate;
        protected TextBox txtExamName;
        protected TextBox txtRubricAMemo;
        protected TextBox txtRubricASum;
        protected TextBox txtRubricDocAA;
        protected TextBox txtStartDate;
        protected UpdatePanel UPPart;

        protected void butDefault_Click(object sender, EventArgs e)
        {
            string strSql = "";
            int num = 0;
            if (this.txtRubricASum.Text.Trim() != "")
            {
                try
                {
                    num = Convert.ToInt32(this.txtRubricASum.Text.Trim());
                }
                catch
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckCB", "alert('测评试题数输入不合法！')", true);
                    return;
                }
                if ((num == 0) || (this.txtRubricDocAA.Text != ""))
                {
                    if (Convert.ToDateTime(this.txtStartDate.Text) > Convert.ToDateTime(this.txtEndDate.Text))
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckAD", "alert('考试有效期限不合法！')", true);
                    }
                    else
                    {
                        string str2;
                        string str3 = num.ToString();
                        string str4 = this.txtRubricAMemo.Text.Trim();
                        string str5 = this.txtStartDate.Text.Trim() + " " + this.DDLStartH.SelectedItem.Value + ":" + this.DDLStartM.SelectedItem.Value + ":00";
                        string str6 = this.txtEndDate.Text.Trim() + " " + this.DDLEndH.SelectedItem.Value + ":" + this.DDLEndM.SelectedItem.Value + ":00";
                        if (this.intID == 0)
                        {
                            strSql = "insert into WorkPaperInfo(ExamName,SubjectName,SubjectID,RubricDist,MemoDist,PaperStartTime,PaperEndTime,InputMan,InputUserCode)";
                            strSql = strSql + " values('" + this.publicFun_0.CheckString(this.txtExamName.Text) + "','" + this.strSubjectName.Trim().Replace(",", "、").Trim().Replace("│", "").Replace("├", "") + "','" + this.strSubjectID.Trim() + "','" + str3.Trim() + "',";
                            strSql = strSql + "'" + str4.Trim() + "','" + str5.Trim() + "','" + str6.Trim() + "','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
                            this.intID = this.publicFun_0.RetrunExecute(strSql);
                            strSql = ("Insert into WorkPaperData(PaperID,RubricS,RubricD,SortInfo,UserCode) values(" + this.intID) + ",'" + this.txtRubricDocAA.Text.Trim() + "','','ID','')";
                            if (this.publicFun_0.ExecuteSql(strSql) <= 0)
                            {
                                strSql = string.Concat(new object[] { "Delete from WorkPaperInfo where ID=", this.intID, ";Delete from WorkPaperData where PaperID=", this.intID });
                                this.publicFun_0.ExecuteSql(strSql);
                                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckAE", "alert('新建测评试卷信息失败！')", true);
                            }
                            else
                            {
                                str2 = base.Request.Url.ToString();
                                str2 = str2.Substring(0, str2.LastIndexOf('/') + 1);
                                base.Response.Redirect(str2 + "AddWorkExamReule.aspx?ID=" + this.intID);
                            }
                        }
                        else
                        {
                            strSql = "update WorkPaperInfo set ExamName='" + this.publicFun_0.CheckString(this.txtExamName.Text) + "',SubjectName='" + this.strSubjectName.Trim().Replace(",", "、").Replace("│", "").Replace("├", "") + "',SubjectID='" + this.strSubjectID.Trim() + "',RubricDist='" + str3.Trim() + "'";
                            strSql = string.Concat(new object[] { strSql, ",MemoDist='", str4.Trim(), "',PaperStartTime='", str5.Trim(), "',PaperEndTime='", str6.Trim(), "',InputMan='", this.httpCookie_1.Value, "',InputUserCode='", this.httpCookie_0.Value, "' where ID=", this.intID });
                            strSql = string.Concat(new object[] { strSql, ";update WorkPaperData set PaperID=", this.intID, ",RubricS='", this.txtRubricDocAA.Text.Trim(), "',RubricD='',SortInfo='ID#ID' where UserCode='' and PaperID=", this.intID });
                            this.publicFun_0.ExecuteSql(strSql);
                            if (this.publicFun_0.ExecuteSql(strSql) <= 0)
                            {
                                strSql = string.Concat(new object[] { "Delete from WorkPaperInfo where ID=", this.intID, ";Delete from WorkPaperData where PaperID=", this.intID });
                                this.publicFun_0.ExecuteSql(strSql);
                                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckAE", "alert('新建测评试卷信息失败！')", true);
                            }
                            else
                            {
                                str2 = base.Request.Url.ToString();
                                str2 = str2.Substring(0, str2.LastIndexOf('/') + 1);
                                base.Response.Redirect(str2 + "AddWorkExamReule.aspx?ID=" + this.intID);
                            }
                        }
                    }
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckAC", "alert('请选择测评试题！')", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckCC", "alert('请输入测评试题数！')", true);
            }
        }

        protected void butShowRubricCount_Click(object sender, EventArgs e)
        {
            this.method_1();
        }

        private void method_0()
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            connection.Open();
            SqlCommand command = new SqlCommand("select a.*,b.RubricS from WorkPaperInfo a,WorkPaperData b where a.ID=b.PaperID and a.ID=" + this.intID, connection);
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.Read())
            {
                this.txtExamName.Text = reader["ExamName"].ToString();
                this.strSubjectName = reader["SubjectName"].ToString().Replace("、", ",");
                this.strSubjectID = reader["SubjectID"].ToString().Replace("、", ",");
                string[] strArray = reader["RubricDist"].ToString().Split(new char[] { '#' });
                string[] strArray2 = reader["MemoDist"].ToString().Split(new char[] { '#' });
                this.txtRubricASum.Text = strArray[0].ToString();
                this.txtRubricAMemo.Text = strArray2[0].ToString();
                this.txtRubricDocAA.Text = reader["RubricS"].ToString();
                this.txtStartDate.Text = DateTime.Now.ToShortDateString();
                this.txtEndDate.Text = DateTime.Now.AddDays(7.0).ToShortDateString();
            }
            reader.Close();
            command.Dispose();
            connection.Close();
            connection.Dispose();
        }

        private void method_1()
        {
            this.strSubjectName = base.Request.Form["txtSubjectName"].ToString().Trim();
            this.strSubjectID = base.Request.Form["txtSubjectID"].ToString().Trim();
            string[] strArray = this.strSubjectID.Split(new char[] { ',' });
            string str = "";
            for (int i = 0; i < strArray.Length; i++)
            {
                if (strArray[i].Trim() != "")
                {
                    if (str.Trim().Length != 0)
                    {
                        str = str + " or SubjectID='" + strArray[i].Trim() + "' ";
                    }
                    else
                    {
                        str = " (SubjectID='" + strArray[i].Trim() + "' ";
                    }
                }
            }
            if (!(str.Trim() != ""))
            {
                str = " ID<>0 ";
            }
            else
            {
                str = str + ")";
            }
            if (strArray.Length > 0)
            {
                this.labSumA.Text = "题库共<font color=red>" + this.publicFun_0.GetValues("select count(*) as RubricCount from WorkRubric where RubricType='A' and IsDel='N' and  " + str + " ", "RubricCount") + "</font>道";
            }
            else
            {
                this.labSumA.Text = "题库共<font color=red>0</font>道";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_2 = new HttpCookie("ExamUserLevel");
            this.httpCookie_2 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_3 = new HttpCookie("ExamDeptID");
            this.httpCookie_3 = base.Request.Cookies["ExamDeptID"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.intID = Convert.ToInt32(base.Request["ID"].ToString());
                if (Convert.ToInt32(ConfigurationManager.ConnectionStrings["OnlineNum"].ConnectionString) == 0x3a)
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BTHB", "alert('该版本为特惠版，此功能未开放！')", true);
                }
                else if (!base.IsPostBack)
                {
                    if (this.publicFun_0.ReturnPaperData() == "Yes")
                    {
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select AddPaper,ManagPaper from UserPower where UserCode='" + this.httpCookie_0.Value + "'", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        if (reader.Read())
                        {
                            this.ViewState["AddPower"] = reader["AddPaper"].ToString();
                            this.ViewState["ManagPower"] = reader["ManagPaper"].ToString();
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                        this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
                        if (!(this.ViewState["AddPower"].ToString() != "Y") || !(this.ViewState["ManagPower"].ToString() != "Y"))
                        {
                            this.butDefault.Attributes.Add("onclick", "javascript:{if(confirm('确定试卷参数无误，要进行下一步吗？')==false) return false;}");
                            this.txtStartDate.Text = DateTime.Now.ToShortDateString();
                            this.txtEndDate.Text = DateTime.Now.AddDays(1.0).ToShortDateString();
                            this.labChoose.Text = "<a href='#' onclick=javascript:ShowChooseRubric();>" + this.labChoose.Text.Trim() + "</a>";
                            if (this.intID != 0)
                            {
                                this.method_0();
                            }
                        }
                        else
                        {
                            this.butDefault.Enabled = false;
                            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('随机组卷权限未开放！')", true);
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                    }
                }
                else
                {
                    this.strSubjectName = base.Request.Form["txtSubjectName"].ToString().Trim();
                    this.strSubjectID = base.Request.Form["txtSubjectID"].ToString().Trim();
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
    }
}

